/*
 * @Author: Yhd you@example.com
 * @Date: 2023-02-07 22:25:56
 * @LastEditors: yan_hai_dong 891236760@qq.com
 * @LastEditTime: 2023-03-13 18:43:06
 * @FilePath: \get-comment\on.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

function info() {
    try {
        let img = getVideoToBase64Img()
        // console.log(img)
        // next()
        requer('/test/img', {
            method: 'POST',
            data: {
                img
            }
        }).then(res => {
            let {
                code,
                data
            } = res;
            if (code == 200) {
                let {
                    ImageWidth,
                    ImageHeight,
                    FaceInfos: [{
                        X,
                        Y,
                        Width,
                        Height,
                        FaceAttributesInfo: {
                            Gender,
                            Age,
                            Expression,
                            Glass,
                            Pitch,
                            Yaw,
                            Roll,
                            Beauty,
                            Hat,
                            Mask,
                            Hair: {
                                Bang,
                                Color,
                                Length,
                            },
                            EyeOpen
                        },
                        FaceQualityInfo: {
                            Score,
                            Sharpness,
                            Brightness,
                            Completeness: {
                                Eyebrow,
                                Eye,
                                Nose,
                                Cheek,
                                Mouth,
                                Chin,
                            }
                        },
                    }],
                    FaceModelVersion,
                } = data
                console.log({
                    Gender,
                    Beauty
                })
                if (Gender < 50 && Beauty >= 60) {
                    document.querySelector('.swiper-slide.swiper-slide-active').querySelector(' .H2HjqV3h').click();
                    document.querySelector('.swiper-slide.swiper-slide-active').querySelector(' .HNBvVrcV').click();
                    return next('这个可以 关注了')
                }
            }
            next()
        }).catch(err => {
            next()
        })
    } catch (error) {
        next()
    }
}
let falg = false;

function next(msg = '这个不行 下一个') {
    if (!falg) return;
    console.log(msg)
    setTimeout(() => {
        var e = new KeyboardEvent('keydown', {
            'keyCode': 40,
            'which': 40
        });
        document.dispatchEvent(e)
        setTimeout(() => {
            console.log('info')
            info();
        }, 800)
    }, 1500)
}

function getVideoToBase64Img() {
    const video = document.querySelector('.swiper-slide.swiper-slide-active').querySelector('video')
    video.setAttribute('crossOrigin', 'anonymous')
    var canvas = document.createElement("canvas");
    canvas.width = video.clientWidth;
    canvas.height = video.clientHeight;
    const ctx = canvas.getContext("2d")
    ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
    var base64 = canvas.toDataURL("image/png");
    if (base64.size < 80000)
        return base64
    else {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        const maxSize = 150 * 150;
        const size = video.clientWidth * video.clientHeight;
        const sf = Math.sqrt(maxSize / size);
        canvas.width = video.clientWidth;
        canvas.height = video.clientHeight;
        canvas.getContext("2d").drawImage(video, 0, 0, video.clientWidth * sf, video.clientHeight * sf);
        var base64 = canvas.toDataURL("image/png");
        return base64
    }
}

function requer(url, {
    method = 'GET',

    data = {}
}) {
    return fetch(`https://3tg0910673.yicp.fun${url}`, {
        method,
        body: JSON.stringify(data),
        cors: 'no cors',
        headers: {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*'
        },
    }).then((res) => {
        if (res.status === 200) {
            return res.json()
        } else return {}
    })
}

chrome.runtime.onMessage.addListener((message) => {
    if (message.extension == 'ON') {
        falg = true;
        info();
    } else {
        falg = false
        console.log('关闭');
    }
})